//
//  DiscoverView.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

struct DiscoverView: View {
    @EnvironmentObject var discoverViewModel: DiscoverViewModel
    
    var body: some View {
        NavigationView {
            List {
                // 朋友圈
                NavigationLink(destination: MomentsView().environmentObject(discoverViewModel)) {
                    DiscoverMenuItemView(
                        icon: "camera.circle.fill",
                        iconColor: .blue,
                        title: "朋友圈",
                        badge: 3
                    )
                }
                
                Section {
                    DiscoverMenuItemView(
                        icon: "qrcode",
                        iconColor: .green,
                        title: "扫一扫"
                    ) {
                        print("扫一扫")
                    }
                    
                    DiscoverMenuItemView(
                        icon: "location.circle.fill",
                        iconColor: .orange,
                        title: "附近的人"
                    ) {
                        print("附近的人")
                    }
                }
                
                Section {
                    DiscoverMenuItemView(
                        icon: "gamecontroller.fill",
                        iconColor: .red,
                        title: "游戏"
                    ) {
                        print("游戏")
                    }
                    
                    DiscoverMenuItemView(
                        icon: "app.gift.fill",
                        iconColor: .purple,
                        title: "小程序"
                    ) {
                        print("小程序")
                    }
                }
                
                Section {
                    DiscoverMenuItemView(
                        icon: "music.note",
                        iconColor: .orange,
                        title: "音乐"
                    ) {
                        print("音乐")
                    }
                    
                    DiscoverMenuItemView(
                        icon: "tv.fill",
                        iconColor: .blue,
                        title: "视频号"
                    ) {
                        print("视频号")
                    }
                }
                
                Section {
                    DiscoverMenuItemView(
                        icon: "bag.fill",
                        iconColor: .green,
                        title: "购物"
                    ) {
                        print("购物")
                    }
                }
            }
            .navigationTitle("发现")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct DiscoverMenuItemView: View {
    let icon: String
    let iconColor: Color
    let title: String
    let badge: Int?
    let action: (() -> Void)?
    
    init(icon: String, iconColor: Color, title: String, badge: Int? = nil, action: (() -> Void)? = nil) {
        self.icon = icon
        self.iconColor = iconColor
        self.title = title
        self.badge = badge
        self.action = action
    }
    
    var body: some View {
        Button(action: {
            action?()
        }) {
            HStack(spacing: 15) {
                Image(systemName: icon)
                    .font(.system(size: 20))
                    .foregroundColor(iconColor)
                    .frame(width: 25)
                
                Text(title)
                    .font(.system(size: 16))
                    .foregroundColor(.primary)
                
                Spacer()
                
                if let badge = badge, badge > 0 {
                    Text("\(badge)")
                        .font(.system(size: 12, weight: .medium))
                        .foregroundColor(.white)
                        .frame(minWidth: 18, minHeight: 18)
                        .background(Color.red)
                        .clipShape(Circle())
                }
                
                if action != nil {
                    Image(systemName: "chevron.right")
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                }
            }
            .padding(.vertical, 2)
        }
        .buttonStyle(PlainButtonStyle())
        .disabled(action == nil)
    }
}
